<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------
namespace app\services\care;

use app\model\care\ServiceCate;
use app\model\care\ServiceItem;
use app\model\care\ServicePlace;
use app\services\BaseServices;
use app\dao\care\ServiceItemDao;
use crmeb\exceptions\AdminException;
use think\exception\ValidateException;
use think\facade\Db;

class ServiceItemServices extends BaseServices
{
    /**
     * ExamQuestionServices constructor.
     * @param ServiceItemDao $dao
     */
    public function __construct(ServiceItemDao $dao)
    {
        $this->dao = $dao;
    }

    /**
     * 列表
     * @param array $where
     * @param bool $is_page
     * @return array
     */
    public function getList(array $where = [],bool $is_page = false){
        $page = $limit = 0;
        if($is_page) [$page,$limit] = $this->getPageValue();
        $list = $this->dao->getList($where,$page,$limit,$is_page);
        foreach ($list['list'] as &$item){
            $item['add_time'] = date('Y-m-d H:i:s',$item['add_time']);
        }
        return $list;
    }

    /**
     * 保存
     * @param array $data
     * @return string
     */
    public function save(array $data){
        if($this->get($data['id'])) {
            if($this->dao->update($data['id'],$data)) return '保存成功';
            throw new AdminException('保存失败');
        } else {
            $data['add_time'] = time();
            $data['is_del'] = 0;
            if($this->dao->save($data)) return '添加成功';
            throw new AdminException('添加失败');
        }
    }

    /**
     * 删除
     * @param int $id
     * @return array
     */
    public function del(int $id){
        if($this->get($id)) return $this->dao->del($id);
        throw new AdminException('删除失败');
    }

    /**
     * @param array $data
     * @return string
     */
    public function item_type(array $data) {
        if($this->get($data['id'])){
            $this->dao->update($data['id'],$data);
            return "修改成功";
        }
        throw new AdminException('修改失败!');
    }

    /**
     * 是否显示
     * @param array $data
     * @return string
     */
    public function status(array $data) {
        if($this->get($data['id'])){
            $this->dao->update($data['id'],$data);
            return "修改成功";
        }
        throw new AdminException('修改失败!');
    }

    /**
     * 是否推荐
     * @param array $data
     * @return string
     */
    public function is_recom(array $data) {
        if($this->get($data['id'])){
            $this->dao->update($data['id'],$data);
            return "修改成功";
        }
        throw new AdminException('修改失败!');
    }

    /**
     * 获取详情
     * @param int $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function read(int $id)
    {
        $info = $this->dao->read($id);
        return $info;
    }

    /**
     * 项目级联数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function itemCascader(){
        $cate = ServiceCate::field('id,title')->where('is_del',0)->select()->toArray();
        $data = [];
        $i = 0;
        foreach ($cate as $cateItem){
            $item = ServiceItem::field('id,title')->where(['cate_id'=>$cateItem['id'],'is_del'=>0,'status'=>1])->select()->toArray();
            if($item){
                array_push($data,['value'=>$cateItem['id'],'label'=>$cateItem['title'],'children'=>[]]);
                foreach ($item as $itemItem){
                    array_push($data[$i]['children'],['value'=>$itemItem['id'],'label'=>$itemItem['title']]);
                }
                $i++;
            }
        }
        return $data;
    }

    /**
     * 获取服务项目
     * @param int $cate_id
     * @param $data
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getItemList(int $cate_id,$data){
        [$page,$limit] = $this->getPageValue();
        $item = $this->dao->getItemList($cate_id,$data,$page,$limit);
        return $item;
    }

    /**
     * 服务详情
     * @param int $item_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getItemDetail(int $item_id){
        $info = Db::name('service_item')->where(['id'=>$item_id])->find();
        if($info['is_del']) throw new ValidateException('项目已被删除，请重新选择');
        if(!$info['status']) throw new ValidateException('项目已禁止下单，请重新选择');
        return $info;
    }

    /**
     * 获取服务列表
     * @param array $where
     * @param bool $is_page
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAllServiceItem(array $where,bool $is_page = false){
        $page = $limit = 0;
        if($is_page){
            [$page,$limit] = $this->getPageValue();
        }
        $list = $this->dao->getAllServiceItem($where,$page,$limit);
        $placeServices = app()->make(ServicePlaceServices::class);
        $exist = $placeServices->getPlaceItem($where['place_id']);
        foreach ($list['list'] as &$item){
            $item['is_exist'] = in_array($item['id'],array_column($exist,'item_id'));
        }
        return $list;
    }

    /**
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getExistItem($where){
        $exist_place = ServicePlace::field('item_id')->where('place_id',$where['place_id'])->select()->toArray();
        $arr = [];
        foreach ($exist_place as $item){
            $arr[] = $item['item_id'];
        }
        if(!$arr){
            return [];
        }
        $item_id = join(',',$arr);
        $item_list = ServiceItem::where('id','in',$item_id)->where('is_del',0);
        if($where['cate_id']){
            $item_list->where('cate_id',$where['cate_id']);
        }
        $item_list = $item_list->order('sort desc')->select()->toArray();
        return $item_list;
    }

    /**
     * 服务下拉
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getItemSelect(){
        $service_item = ServiceItem::where(['is_del'=>0,'status'=>1,'is_order'=>1])->field('id as value,title as label')->order('sort desc')->select()->toArray();
        return $service_item;
    }
}
